package com.Xht.www.dao;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TurnoverInfoDAO extends BaseDAO {

    // 根据商家名称更新营业额
    public void updateTurnoverByMerchant(Connection connection, String merchantName) throws SQLException {
        // 计算商家总营业额
        BigDecimal totalTurnover = calculateTotalTurnoverByMerchant(connection, merchantName);

        // 检查是否已有营业额记录
        boolean exists = checkTurnoverRecordExists(connection, merchantName);

        if (exists) {
            // 更新现有记录
            updateExistingTurnover(connection, merchantName, totalTurnover);
        } else {
            // 插入新记录
            insertNewTurnover(connection, merchantName, totalTurnover);
        }
    }

    // 计算商家总营业额
    private BigDecimal calculateTotalTurnoverByMerchant(Connection connection, String merchantName) throws SQLException {
        String sql = "SELECT SUM(price * sales_volume) FROM room_type_info WHERE merchant_name = ?";
        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, merchantName);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return rs.getBigDecimal(1);
                }
            }
        }
        return BigDecimal.ZERO;
    }

    // 检查营业额记录是否存在
    private boolean checkTurnoverRecordExists(Connection connection, String merchantName) throws SQLException {
        String sql = "SELECT COUNT(*) FROM turnover_info WHERE merchant_name = ?";
        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, merchantName);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return rs.getInt(1) > 0;
                }
            }
        }
        return false;
    }

    // 更新现有营业额记录
    private void updateExistingTurnover(Connection connection, String merchantName, BigDecimal totalTurnover) throws SQLException {
        String sql = "UPDATE turnover_info SET amount = ? WHERE merchant_name = ?";
        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setBigDecimal(1, totalTurnover);
            pstmt.setString(2, merchantName);
            pstmt.executeUpdate();
        }
    }

    // 插入新营业额记录
    private void insertNewTurnover(Connection connection, String merchantName, BigDecimal totalTurnover) throws SQLException {
        String sql = "INSERT INTO turnover_info (amount, merchant_name) VALUES (?, ?)";
        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setBigDecimal(1, totalTurnover);
            pstmt.setString(2, merchantName);
            pstmt.executeUpdate();
        }
    }

    // 根据商家名称获取营业额
    public BigDecimal getTurnoverByMerchant(Connection connection, String merchantName) {
        String sql = "SELECT COALESCE(SUM(amount), 0) FROM turnover_info WHERE merchant_name = ?";
        boolean closeConnection = connection == null;

        try {
            if (closeConnection) {
                connection = getConnection();
            }

            try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
                pstmt.setString(1, merchantName);
                try (ResultSet rs = pstmt.executeQuery()) {
                    if (rs.next()) {
                        return rs.getBigDecimal(1);
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (closeConnection && connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return BigDecimal.ZERO;
    }

}